#include <QPolygonF>

#include "drawroaditem.h"


DrawRoadItem::DrawRoadItem(QObject *parent): QObject(parent)
{
//    setAcceptedMouseButtons(Qt::LeftButton);
//    setFlag(QGraphicsItem::ItemIsSelectable);
//    setAcceptedMouseButtons(Qt::NoButton);
    setFlag(QGraphicsPathItem::ItemIsSelectable);
    setAcceptHoverEvents(true);
//    setAcceptTouchEvents(true);

    m_pSplinecurves = new Splinecurves();
}

void DrawRoadItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    painter->setBrush(QColor(255, 255, 0, 100));
//    painter->setPen(QPen(Qt::red, 0.1));

//    QPolygonF polygon;

////    setPos();

//    qDebug() << "Begin Paint, count is: " << m_qvecRoadPoints.size();

//    for(QPointF point: m_qvecRoadPoints)
//    {
//        qDebug() << "paint" << point;
//        polygon << mapFromScene(point);
//    }

//    qDebug() << "End Paint, count is: " << m_qvecRoadPoints.size();

//    this->setPolygon(polygon);
//    painter->drawPolygon(polygon);

    m_pSplinecurves->drawSpline(*painter);
}

QPointF DrawRoadItem::getLink() const
{
    return QPointF(); //接口，作为画连接的连接点
}

void DrawRoadItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//    setPoint(event->pos());    
    qDebug() << "Mouse Press: " << event->pos();

    // 单击鼠标左键获取控制点
    if (event->buttons() == Qt::LeftButton)
    {
        m_pSplinecurves->addCtrlPoint(event->pos());
    }
    // 单击鼠标右键清空控制点
    else if (event->buttons() == Qt::RightButton)
    {
        m_pSplinecurves->clearCtrlPoint();
    }

    update();

    QGraphicsItem::mousePressEvent(event);
}

void DrawRoadItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
//    setPoint(event->pos());

    qDebug() << "Mouse Release: " << event->pos();

    QGraphicsItem::mouseReleaseEvent(event);
}

void DrawRoadItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() << "Mouse Move: " << event->pos();

    QGraphicsItem::mouseMoveEvent(event);
}

void DrawRoadItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << "hoverMouse Move: " << event->pos();

    QGraphicsItem::hoverMoveEvent(event);
}
